package com.cheng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cheng.common.exception.CommonException;
import com.cheng.common.utils.StringUtil;
import com.cheng.data.entity.UserInfoEntity;
import com.cheng.data.entity.UserRoleEntity;
import com.cheng.data.mapper.UserRoleMapper;
import com.cheng.service.UserRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: ChengJW
 * @date: 2021/11/14 14:26
 * @version: 1.0
 * @description: 用户-角色中间表操作
 */
@Service
public class UserRoleServiceImpl implements UserRoleService {

    private final static Logger log = LoggerFactory.getLogger(UserRoleServiceImpl.class);

    @Autowired
    private UserRoleMapper userRoleMapper;

    /**
    * @author: ChengJW
    * @params: [entity]
    * @return: int
    * @date: 2021/11/14
    * @description: 插入用户-角色中间信息
    */
    @Override
    public int insertUserRoleInfo(UserRoleEntity entity) {
        return userRoleMapper.insert(entity);
    }

    /**
    * @author: ChengJW
    * @params: [entity]
    * @return: int
    * @date: 2021/11/14
    * @description: 修改用户-角色信息
    */
    @Override
    public int updateUserRoleInfo(UserRoleEntity entity) throws CommonException {
        return userRoleMapper.updateById(entity);
    }

    /**
    * @author: ChengJW
    * @params: [entity]
    * @return: java.util.List<com.cheng.data.entity.UserRoleEntity>
    * @date: 2021/11/14
    * @description: 查询 用户-角色信息表
    */
    @Override
    public List<UserRoleEntity> queryUserRoleInfo(UserRoleEntity entity) throws CommonException {

        QueryWrapper<UserRoleEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtil.isNotNull(entity.getUlId()),"ul_id",entity.getUlId());
        wrapper.eq(StringUtil.isNotNull(entity.getRoId()),"ro_id",entity.getRoId());
        wrapper.eq(StringUtil.isNotNull(entity.getUrId()),"ur_id",entity.getUrId());
        return userRoleMapper.selectList(wrapper);
    }

    /**
    * @author: ChengJW
    * @params: [entity]
    * @return: void
    * @date: 2021/11/14
    * @description: 删除用户-角色信息
    */
    @Override
    public void deleteUserRoleInfo(UserRoleEntity entity) throws CommonException {
        QueryWrapper<UserRoleEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtil.isNotNull(entity.getUrId()),"ur_Id",entity.getUrId());
        userRoleMapper.delete(wrapper);
    }
}
